//
#ifndef __ARK_THREAD_POOL_H__
#define __ARK_THREAD_POOL_H__

#include <stdlib.h>
#include <stdio.h>
#include <vector>
#include <deque>
#include <list>
#include <thread>
#include <deque>
#include <mutex>
#include <functional>

namespace Ark
{
    class ThreadPool
    {
        typedef ThreadPool self;
    public:
        ThreadPool();
        virtual ~ThreadPool();
        ThreadPool(const ThreadPool&) = delete;
        void operator = (const ThreadPool&) = delete;

    public:
        bool Start(unsigned threadCount);
        bool Stop();
        bool Post(const std::function<void()> &task);
        bool Post(const std::function<void()> &&task);
        size_t GetTaskCount() const;

    private:
        struct Task;

        void _StartWorkingThreads(unsigned threadCount);
        bool _Post(Task* work);
        void _WorkLoop();

    private:
        unsigned _threadCount = 0;
        volatile bool _running = false;
        mutable std::mutex _mutex;
        std::condition_variable _cond;
        std::list<Task*> _taskList;
        std::vector<std::shared_ptr<std::thread>> _threadList;
    };
}

#endif //__ARK_THREAD_POOL_H__
